package com.lz.sports.ui.config

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.lz.comm.BR
import com.lz.library.base.BaseActivity
import com.lz.library.extension.click
import com.lz.library.extension.showToast
import com.lz.sports.R
import com.lz.sports.adapter.DeviceEnterAdapter
import com.lz.sports.databinding.ActDeviceEnterBinding
import com.lz.sports.dialog.DelayedLoadingDialog
import com.lz.sports.dialog.WarningDialog
import com.lz.sports.utils.ToastUtils
import com.lz.sports.vm.config.DeviceEnterViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//设备录入
class DeviceEnterActivity : BaseActivity<DeviceEnterViewModel, ActDeviceEnterBinding>(),
    WarningDialog.OnWarningConfirmListener {

    private val adapter by lazy { DeviceEnterAdapter() }

    override fun getLayoutRes(): Int = R.layout.act_device_enter

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.deviceInfo.observe(this) {
            adapter.addItem(item = it)
        }
        viewModel.delayEvent.observe(this) {
            DelayedLoadingDialog.show(this, it)
        }
    }

    override fun initData(savedState: Bundle?) {
        adapter.setOnItemClickListener(object : com.lz.library.adapter.BaseAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                adapter.getItem(position).select()
            }
        })
        binding.rvList.adapter = adapter

        lifecycleScope.launch {
            arrayOf(binding.tvReset, binding.tvSelect, binding.tvInvert, binding.flSearch, binding.tvSave)
                .click().collect { onClick(it) }
        }
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.tv_reset -> {
                if (viewModel.isSearching.value == true) {
                    WarningDialog.show(this, resources.getString(R.string.device_enter_reset))
                } else {
//                    R.string.resetSuccess.showToast()
                }
            }
            R.id.tv_select -> adapter.data.forEach { it.needSelect() }
            R.id.tv_invert -> adapter.data.forEach { it.select() }
            R.id.fl_search -> {
                if (v.isSelected) {
                    viewModel.stopSearch()
                    v.isSelected = false
                } else {
                    adapter.removeAll()
                    viewModel.search()
                    v.isSelected = true
                }
            }
            R.id.tv_save -> {
                val selected = adapter.data.filter { it.hasSelected() }
                if (selected.isEmpty()) {
                    ToastUtils.showMyToast(R.string.device_enter_un)
                    return
                }
                viewModel.save(selected)
            }
        }
    }

    override fun onConfirm(dialog: WarningDialog, requestCode: Int) {
        dialog.dismiss()
        binding.flSearch.isSelected = false
        viewModel.stopSearch()
        R.string.resetSuccess.showToast()
    }
}